package trefliq.EJB.sessionbeans;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import javax.annotation.security.PermitAll;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import trefliq.EJB.entities.Cart;
import trefliq.EJB.entities.CartItem;
import trefliq.EJB.entities.Customers;
import trefliq.EJB.entities.Movie;

/**
 *
 * @author Brandon
 */
@Stateless
@LocalBean
@PermitAll // (by default, allow all, restrict what's necessary)
public class CustomersSession {

   @PersistenceContext(unitName="trefliqPU")
   private EntityManager em;
   
   public Customers createCustomer(Customers e) { 
       em.persist(e);
       return e; 
   }
   
   //TODOSEC: @RolesAllowed({SecurityRole.ADMIN, SecurityRole.EMPLOYEE})
   public Customers deleteCustomer(Customers c) { 
       Customers customer = em.find(Customers.class, c.getId());
       em.remove(customer);
       return customer;
   }
   
   //TODOSEC: @RolesAllowed({SecurityRole.ADMIN, SecurityRole.EMPLOYEE})
   public List<Customers> getAllCustomers() {
       return em.createNamedQuery("findAllCustomers", Customers.class).getResultList();
   }
   
   public Customers getCustomerById(Long customerId) { 
        return em.createNamedQuery("findCustomerById", Customers.class).
                    setParameter("id", customerId).getSingleResult();
       
   }
   
   public Customers getCustomerByPersonId(Long personId) { 
       return em.createNamedQuery("findCustomerByPersonId", Customers.class).
                    setParameter("personId", personId).getSingleResult();
   }
   
   public void addCart(Long customerId) { 
       Customers c = em.createNamedQuery("findCustomerById", Customers.class).
                    setParameter("id", customerId).getSingleResult();
       Cart cart = new Cart(); 
       cart.setCustomerid(c);
       cart.setStatus(Cart.Status.Ordered);
       em.persist(cart);
       Cart managedCart = em.find(Cart.class, cart.getId());
       c.addCart(managedCart);
   }
   
   public void deleteCart(Long customerId, Cart cartId) { 
       Customers c = em.createNamedQuery("findCustomerById", Customers.class).
                    setParameter("id", customerId).getSingleResult();
       Cart cart = em.find(Cart.class, cartId.getId());
       c.deleteCart(cart);
   }
   
   public void addMovie(Long customerId, Cart cartId, Movie movie) { 
       Customers c = em.createNamedQuery("findCustomerById", Customers.class).
                    setParameter("id", customerId).getSingleResult();
       Cart cart = em.find(Cart.class, cartId.getId());
       em.persist(movie); 
       CartItem item = new CartItem(); 
       item.setCartid(cart);
       item.setMovieid(movie);
       em.persist(item);
       CartItem managedItem = em.createNamedQuery("CartItem.findById", CartItem.class).
                    setParameter("id", item.getId()).getSingleResult();
       
       cart.addCartItem(managedItem);         
   }
   
   public void deleteMovie(Long customerId, Cart cartId, Movie movie) { 
       Customers c = em.createNamedQuery("findCustomerById", Customers.class).
                    setParameter("id", customerId).getSingleResult();  
       Cart cart = em.find(Cart.class, cartId.getId());
       Collection<CartItem> managedItem = cart.getCartItemCollection();
       
       CartItem i = null; 
       for (CartItem item : managedItem) 
           if (item.getMovieid().equals(movie))
               i = em.createNamedQuery("CartItem.findById", CartItem.class).
                    setParameter("id", item.getId()).getSingleResult();
        System.out.println(i.toString()); 
        cart.deleteCartItem(i);
   } 
}